package com.atguigu.vodservice.controller;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;

import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.atguigu.baseservice.handler.GuliException;
import com.atguigu.commonutils.R;
import com.atguigu.vodservice.utils.AliyunVodSDKUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "视频管理")
@RestController
@CrossOrigin
@RequestMapping("/eduvod/vod")
public class VideoAdminController {

    @ApiOperation(value = "上传视频")
    @PostMapping("uploadVideo")
    public R uploadVideo(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            String name = file.getOriginalFilename();
            String title = name.substring(0, name.lastIndexOf("."));
            UploadStreamRequest request = new UploadStreamRequest(
                    "LTAI5tSRHzjJQNdgAutYbJ4i",
                    "JVkQmKOorW9nsOlnFkuUBdbODPBOOK",
                    title,
                    name,
                    inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            String videoId = response.getVideoId();
            return R.ok().data("videoId", videoId);
        } catch (IOException e) {
            e.printStackTrace();
            throw new GuliException(2001, "上传失败");
        }
    }

    @ApiOperation(value = "删除视频")
    @DeleteMapping("delVideo/{videoId}")
    public R delVideo(@PathVariable("videoId") String videoId) {
        try {
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient("LTAI5tSRHzjJQNdgAutYbJ4i",
                    "JVkQmKOorW9nsOlnFkuUBdbODPBOOK");
            DeleteVideoRequest request = new DeleteVideoRequest();
            //  DeleteVideoResponse response=new DeleteVideoResponse();
            request.setVideoIds(videoId);
            client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001, "删除失败");
        }
        return R.ok();

    }

    @ApiOperation(value = "批量删除视频")
    @DeleteMapping("delVideoList/")
    public R delVideoList(@PathVariable("videoList") List<String> videoIdList) {
        try {
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient("LTAI5tSRHzjJQNdgAutYbJ4i",
                    "JVkQmKOorW9nsOlnFkuUBdbODPBOOK");
            String videoIds= StringUtils.join(videoIdList.toArray(),",");
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoIds);
            client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001, "批量删除失败");
        }
        return R.ok();
    }
}
